#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2019/5/23 11:55
# @Author  : khz_df
# @Site    : 
# @File    : __init__.py.py
# @Software: ce9nt


import os
import tensorflow as tf
# 只显示 Error
os.environ["TF_CPP_MIN_LOG_LEVEL"] = '3'


def tf3_1():
    a = tf.constant([1.0, 2.0])
    b = tf.constant([3.0, 4.0])

    result = a + b
    print(result)


def tf3_2():
    x = tf.constant([[1.0, 2.0]])
    w = tf.constant([[3.0], [4.0]])

    y = tf.matmul(x, w)
    print(y)


def tf3_2x():
    x = tf.constant([[1.0, 2.0]])
    w = tf.constant([[3.0], [4.0]])

    y = tf.matmul(x, w)
    print(y)

    with tf.Session() as sess:
        print(sess.run(y))


def tf3_foo():
    print(tf.zeros([3, 2], tf.int32))
    print(tf.ones([3, 2], tf.int32))
    print(tf.fill([3, 2], 6))
    print(tf.Variable(tf.random_normal([2, 3], stddev=2, mean=0, seed=1)))
    print(tf.Variable(tf.truncated_normal([3, 3], mean=0, seed=1)))
    print(tf.Variable(tf.random_uniform([4, 3], seed=1)))


def tf3_3():
    """
    P14 3.2-向前传播
    :return:
    """
    x = tf.constant([[0.7, 0.5]])
    # x = tf.constant([[1.0, 1.0]])
    w1 = tf.Variable(tf.random_normal([2, 3], stddev=1, seed=1))
    w2 = tf.Variable(tf.random_normal([3, 1], stddev=1, seed=1))

    # 定义前向传播过程
    a = tf.matmul(x, w1)
    y = tf.matmul(a, w2)

    # 用回话计算结果
    with tf.Session() as sess:
        init_op = tf.global_variables_initializer()
        sess.run(init_op)
        print("y in tf3_3 is: %s\n" % sess.run(y))


def tf3_4():
    """
    P14 3.2-向前传播
    :return:
    """
    x = tf.placeholder(tf.float32, shape=(1, 2))
    w1 = tf.Variable(tf.random_normal([2, 3], stddev=1, seed=2))
    w2 = tf.Variable(tf.random_normal([3, 1], stddev=1, seed=1))

    # 定义前向传播过程
    a = tf.matmul(x, w1)
    y = tf.matmul(a, w2)

    # 用会话计算结果
    with tf.Session() as sess:
        init_op = tf.global_variables_initializer()
        sess.run(init_op)
        print("y in tf3_3 is: %s\n" % sess.run(y, feed_dict={x: [[0.7, 0.5]]}))


def tf3_5():
    """
    P14 3.2-向前传播
    :return:
    """
    # 喂入多组数据
    x = tf.placeholder(tf.float32, shape=(None, 2))
    w1 = tf.Variable(tf.random_normal([2, 3], stddev=1, seed=1))
    w2 = tf.Variable(tf.random_normal([3, 1], stddev=1, seed=1))

    # 定义前向传播过程
    a = tf.matmul(x, w1)
    y = tf.matmul(a, w2)

    # 用回话计算结果
    with tf.Session() as sess:
        init_op = tf.global_variables_initializer()
        sess.run(init_op)
        print("y in tf3_3 is: \n%s\n" % sess.run(y, feed_dict={x: [[0.7, 0.5], [0.3, 0.4], [0.4, 0.5]]}))
        print("w1:\n", sess.run(w1))
        print("w2:\n", sess.run(w2))


def tf3_6():
    """
    导入模块，生成模拟数据集
    :return:
    """
    import numpy as np
    BATCH_SIZE = 8
    seed = 23455

    rng = np.random.RandomState(seed)
    X = rng.rand(32, 2)

    Y = [[int(x0 + x1 < 1)] for (x0, x1) in X]
    print("X:%s\n" % X)
    print("Y:%s\n" % Y)

    x = tf.placeholder(tf.float32, shape=(None, 2))
    y_ = tf.placeholder(tf.float32, shape=(None, 1))

    W1 = tf.Variable(tf.random_normal([2, 3], stddev=1, seed=1))
    W2 = tf.Variable(tf.random_normal([3, 1], stddev=1, seed=1))

    a = tf.matmul(x, W1)
    y = tf.matmul(a, W2)

    loss = tf.reduce_mean(tf.square(y - y_))
    train_step = tf.train.GradientDescentOptimizer(0.001).minimize(loss)

    with tf.Session() as sess:
        init_op = tf.global_variables_initializer()
        sess.run(init_op)
        print(sess.run(W1))
        print(sess.run(W2))
        print('\n')

        STEPS = 3000
        for i in range(STEPS):
            start = (i * BATCH_SIZE) % 32
            end = start + BATCH_SIZE
            sess.run(train_step, feed_dict={x: X[start:end], y_: Y[start:end]})
            if i % 500 == 0:
                total_loss = sess.run(loss, feed_dict={x: X, y_: Y})
                print('已经训练了 %d 轮,loss : %g' % (i, total_loss))

        print(sess.run(W1))
        print(sess.run(W2))


def main():
    tf3_5()
    return
    tf3_1()
    tf3_2()
    tf3_2x()
    tf3_foo()


if __name__ == "__main__":
    print("------------------    Enter __main__    ------------------")

    print(u"[Current work directory is : ]\t" + os.getcwd())
    print(u"[Current process ID is : ]\t" + str(os.getpid()))
    print("\n")
    main()

    print("------------------    Leave __main__    ------------------")
